import datetime
from web import models
from django.shortcuts import render, redirect
from django.http import HttpResponse, JsonResponse
from web.forms.project import ProjectModelForm


def project_list(request):
    """项目列表"""
    if request.method == 'GET':
        form = ProjectModelForm(request)
        projects_created = models.Project.objects.filter(creator=request.tracer.user)
        return render(
            request,
            'web/project_list.html',
            {
                'form': form,
                'projects_created': projects_created,
            }
        )
    form = ProjectModelForm(request, data=request.POST)
    if form.is_valid():
        # 验证通过：前端传入项目名、颜色、项目描述，还需要creator值
        form.instance.creator = request.tracer.user
        # 创建项目
        form.save()
        return JsonResponse({'status': True})
    return JsonResponse({'status': False, 'error': form.errors})
